const db = wx.cloud.database()
var appUser = getApp();
import Toast from '@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldid: "",
    oldlist: [],
    loading: true
  },
  onLoad: function (options) {

  },
  onShow: function () {
    this.getList()
  },

  // 根据删除老人
  delete(e) {
    console.log(e.currentTarget.dataset.id)
    wx.cloud.callFunction({
      name: "Oldman",
      data: {
        type: "delete",
        id: e.currentTarget.dataset.id
      }
    }).then(res => {
      wx.showToast({
        title: '删除成功'
      })
      this.getList()
    })
  },

  getList() {
    db.collection("oldmanlist").where({
      _openid: appUser.globalData.userOpenid
    }).get({
      success: res => {
        console.log(res)
        this.setData({
          oldid: res.data._id,
          oldlist: res.data,
          loading: false
        })
      }
    })
  },
  toAdd() {
    wx.navigateTo({
      url: '../addman/addman',
    })
  },
  toHealth(e) {
    db.collection("health").where({
      pid: e.currentTarget.dataset.id
    }).get({
      success: res => {
        if (res.data.length) {
          wx.navigateTo({
            url: '../health/health?id=' + e.currentTarget.dataset.id,
          })
        }else{
          Toast('老人还没有上传过健康信息');
        }
      }
    })
  }

})